#回合制游戏 两人进行到一个房间 互相攻击
import random


class Role():

    def __init__(self,name,max_attack,health=None):
       self.health=health
       self.name=name
       self.max_attack=max_attack

    def take_damage(self,damage):
       self.health -= damage
       print(f"{self.name}剩余{self.health}血量")
       # if self.name <0:
       #     print(f"{self.name}输啦")
       # else:
       #   pass
    def attack_target(self,target):
        damage=random.randint(10,self.max_attack)
        print(f"{self.name}对{target.name}造成了{damage}点伤害")
        target.take_damage(damage)

    def is_alive(self):
        return self.health>0


li=Role("li",23,100)
hai=Role("hai",12,200)

li.attack_target(hai)
hai.attack_target(li)

class Room():
    room_dict = {"00": {"homename": "hhh", "max": 2}}
    def __init__(self):
        # self.auth = False
        self.k=0
        self.l=[]

    # 类方法
    @classmethod
    def creatroom(cls):
        name = input("请创建新房间名：")
        num = int(input("创建房间编号"))
        max_players = int(input("最大房间人数："))
        if num in cls.room_dict:
            print("该房间编号已被占用")
        else:
            print("新房间创建成功,")
        cls.room_dict[num]= {"homename": name, "max": max_players}
        # self.auth = True
        print(cls.room_dict)

    def join_room(self, players):
        print(self.room_dict)
        choice_room=input("请选择你的房间")
        if choice_room in self.room_dict:
           while True:
             print(players)
             choice_player=input("请选择玩家:")
             if choice_player in players :
                 self.l.append(choice_player)
                 print(self.l)
                 print(f"{choice_player}进入{choice_room}号房间")
                 self.k+=1
                 if self.room_dict[choice_room]["max"] ==self.k:
                     print("战斗开始")
                     self.k=0
                     fight2=Game()
                     fight2.fight(self.l)
                 else:
                     print("房间未满，请继续加入玩家")
             else:
                print("该玩家不存在")
                #                 pk=Role()
                #                 pk.attack_target()
           else:
               print("您输入的房间不存在！")


class Game():
    def __init__(self, roles_dict=None):
        if roles_dict is None:
            self.roles_dict={"boss":{"health":300,"max_attack":999},
                             "hai":{"health":180,"max_attack":45}}
        else:
            self.roles_dict = roles_dict

    def start(self):
      while True:
        menu=""""
        1.创建角色
        2.创建房间
        3.准备
        4.开始战斗(不可选)
        5.退出
        """
        print(menu)
        menu_func={"1":self.creat_role,
                   "2":self.creat_room,
                   "3":self.ready}
        choice=input("请输入你的选择：")
        menu_func[choice]()

    def creat_role(self):
         name=input("输入你的角色id:")
         if name in self.roles_dict:
            print("该id已被占用")
         else:
            health=random.randint(120,250)
            max_attack=random.randint(40,50)
            self.roles_dict[name]={"health":health,"max_attack":max_attack}
            print("创建新角色palyer:",name,"hp:",health,"max_attack:",max_attack)
            print(self.roles_dict)

    def creat_room(self):
          Room.creatroom()

    def ready(self):
        ready=Room()
        ready.join_room(self.roles_dict)

    def fight(self,l):
         player1=l[0]
         player2=l[1]
         palyer1=Role(player1,self.roles_dict[player1]["max_attack"],self.roles_dict[player1]["health"])
         palyer2 = Role(player2, self.roles_dict[player2]["max_attack"], self.roles_dict[player2]["health"])
         palyer1.attack_target(palyer2)
         player2.attack_target(palyer1)

    def exit(self):
        pass


open=Game()
open.start()

# 回合制游戏：两人进行到一个房间，相互攻击
# 创建Game类：游戏
#     交互式菜单：
#         创建角色，创建房间，
#         添加角色到房间，
#         房间满员自动开始游戏
#
# 创建Room类：房间
#     房间名，房间编号，房间成员，房间最大人数
#     加入房间：房间满员自动开始游戏(player1, player2 )
#     room.add(player)
#
# 创建Role类：角色
#     初始血量100，初始积分100，
#     名字、血量、积分、当前所在房间
#     攻击 a.attack(b)随机掉血（10-30）
#     role.join(room)
